home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrtools-1.10 / lib / stdio / fcons.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-15  |  1.9 KB  |  69 lines

  1. /* @(#)fcons.c    2.7 98/02/15 Copyright 1986, 1995 J. Schilling */
  2. /*
  3.  *    Copyright (c) 1986, 1995  J. Schilling
  4.  */
  5. /*
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include "io.h"
  23.  
  24. LOCAL    char    *fmtab[] = {
  25.             "",    /* 0    FI_NONE                */
  26.             "r",    /* 1    FI_READ                */
  27.             "w",    /* 2    FI_WRITE        **1)    */
  28.             "r+",    /* 3    FI_READ  | FI_WRITE        */
  29.             "b",    /* 4    FI_NONE  | FI_BINARY        */
  30.             "rb",    /* 5    FI_READ  | FI_BINARY        */
  31.             "wb",    /* 6    FI_WRITE | FI_BINARY    **1)    */
  32.             "r+b",    /* 7    FI_READ  | FI_WRITE | FI_BINARY    */
  33.         };
  34. /*
  35.  * NOTES:
  36.  *    1)    fdopen() guarantees not to create/trunc files in this case
  37.  *
  38.  *    "w"    will create/trunc files with fopen()
  39.  *    "a"    will create files with fopen()
  40.  */
  41.  
  42.  
  43. FILE *_fcons(fd, f, flag)
  44.     register FILE    *fd;
  45.          int    f;
  46.          int    flag;
  47. {
  48.     int    my_gflag = _io_glflag;
  49.  
  50.     if (fd == (FILE *)NULL)
  51.         fd = fdopen(f, fmtab[flag&(FI_READ|FI_WRITE|FI_BINARY)]);
  52.  
  53.     if (fd != (FILE *)NULL) {
  54.         if (flag & FI_APPEND) {
  55.             (void) fseek(fd, 0L, 2);
  56.         }
  57.         if (flag & FI_UNBUF) {
  58.             setbuf(fd, NULL);
  59.             my_gflag |= _IOUNBUF;
  60.         }
  61.         set_my_flag(fd, my_gflag); /* must clear it if fd is reused */
  62.         return (fd);
  63.     }
  64.     if (flag & FI_CLOSE)
  65.         close (f);
  66.  
  67.     return ((FILE *) NULL);
  68. }
  69.